package dm;

/**
 * <p>Title: Domain Model Generator</p>
 *
 * <p>Description: Automatic generation of a domain model</p>
 *
 * <p>Copyright: Copyright (c) 2011</p>
 *
 * <p>Company: UTA</p>
 *
 * @author David Kung
 * @version 1.0
 */
import java.util.*;
import java.io.*;

public class DMComp {
  String fileName;
  ArrayList theList;
  String leftMarker;
  String rightMarker=">";
  char[] punctuations=new char[] {',', '.', '\n', '(', ')', '[', ']', '{', '}',
     ';', ':', '"', '\'', '/'};
  public DMComp() {}

  public DMComp(String fileName) {
        this.fileName = fileName;
        theList = readList(fileName);
        theList = purge(theList);
        theList = sortByLength(theList);
  }
  public DMComp(String fileName, String leftMarker) {
    this(fileName);
    this.leftMarker=leftMarker;
  }
  public DMComp(String fileName, String leftMarker, String rightMarker) {
    this(fileName, leftMarker);
    this.rightMarker=rightMarker;
  }
  public String dePunctuate(String s) {
    String result=s;
    for (int i=0; i<punctuations.length; i++) {
      result=result.replace(punctuations[i], ' ');
    }
    return result;
  }
  public String markIt(String s) {
    return leftMarker + " " +s+" "+rightMarker;
  }

  public ArrayList readList(String fileName) {
    ArrayList al = new ArrayList();
    try {
      BufferedReader br = new BufferedReader(new FileReader(fileName));
      String line = "";
      while ( (line = br.readLine()) != null) {
        al.add(line.trim());
      }
      br.close();
    }
    catch (IOException e) {
      e.printStackTrace();
    }
    return al;
  }

  public ArrayList purge(ArrayList input) {
    if (input == null || input.size() == 0) {
      return input;
    }
    ArrayList result = new ArrayList();
    for (int i = 0; i < input.size(); i++) {
      boolean found = false;
      String s = (String) input.get(i);
      for (int j = 0; j < result.size(); j++) {
        String t = (String) result.get(j);
        if (s.compareTo(t) == 0) {
          found = true;
          break;
        }
      }
      if (!found) {
        result.add(s);
      }
    }
    return result;
  }

  public ArrayList sortByLength(ArrayList input) {
    if (input == null) {
      return null;
    }
    String[] strArray = new String[input.size()];
    input.toArray(strArray);
    Comparator lenCmpt = new Comparator() {
      public int compare(Object obj1, Object obj2) {
        String s1 = "", s2 = "";
        try {
          s1 = (String) obj1;
          s2 = (String) obj2;
        }
        catch (ClassCastException e) {
          e.printStackTrace();
        }
        if (s1.length() == s2.length()) {
          return 0;
        }
        if (s1.length() < s2.length()) {
          return 1;
        }
        return -1;
      }

      public boolean equals(Object obj) {
        return this.equals(obj);
      }
    };
    Arrays.sort(strArray, lenCmpt);
    ArrayList result = new ArrayList();
    for (int i = 0; i < strArray.length; i++) {
      result.add(i, strArray[i]);
    }
    return result;

  }
  public void annotate(Segment desc) {
    Segment p = desc;
    while (p != null) {
      if (p.skip) {
        p = p.next;
        continue;
      }
      if (checkNMark(p)) continue;
      p = p.next;
    }
  }
  public String[] tryThese(String item) {
    // do nothing in super-class
    return new String[] {};
  }
  public int indexOf(String content, String item) {
    String tmp=" "+dePunctuate(content)+" ";
    return tmp.toLowerCase().indexOf(" "+item.toLowerCase()+" ");
  }
  public void split(Segment s, int from, int to) {
      String content=s.getContent();
      if (from == 0) {
        String tailContent = content.substring(to, content.length());
        Segment tailSgmt = new Segment(tailContent, false, s.next);
        s.content=content.substring(0, to);
        s.skip=true;
        s.next = tailSgmt;
        return;
      }
      if (from > 0) {
        String tailContent = content.substring(to, content.length());
        Segment tailSgmt = new Segment(tailContent, false, s.next);
        Segment midSgmt = new Segment(markIt(content.substring(from, to)), true, tailSgmt);
        s.content = content.substring(0, from);
        s.skip = false;
        s.next = midSgmt;
      }
  }
  public boolean checkNMark(Segment p) {
    String content = p.getContent();
    for (int j = 0; j < theList.size(); j++) {
      String item = (String) theList.get(j);
      int k = indexOf(content, item);
      if (k<0) {
        String[] s=tryThese(item);
        for(int i=0; i<s.length; i++) {
          k=indexOf(content, s[i]);
          if (k>=0) {
            item = s[i];
            break;
          }
        }
      }
      if (k>=0) {
        split(p, k, k + item.length());
        return true;
      }
    }
    return false;
  }
  public static void main(String[] args) {
    //DMComp dmcomp = new DMComp();
  }

  class MyComparator
      implements Comparator {
    public int compare(Object o1, Object o2) {
      String s1 = (String) o1, s2 = (String) o2;
      return s1.compareTo(s2);
    }
  }
}
